package com.ninetaildemonfox.zdl.socialproject.mvp.presenter;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.PreservationContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity.CashSuccessActivity;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/28
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class PreservationPresenter {
    public AppCompatActivity me;
    public PreservationContract contract;

    public PreservationPresenter(AppCompatActivity me, PreservationContract contract) {
        this.me = me;
        this.contract = contract;
    }

    public void addWithdraw(String token, String org_money, String service_charge) {
        if (TextUtils.isEmpty(org_money)) {
            ToastUtil.show("提现金额不能为空！");
            return;
        }

        Model.getInstance().getUserApi().addWithdraw(token, org_money, service_charge)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        CashSuccessActivity.start(me);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    public void serviceCharge(String service_charge) {

        Model.getInstance().getUserApi().serviceCharge(service_charge)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        if (data == null) {
                            return;
                        }
                        contract.Success(data);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

}
